<template>
    <el-dialog class="reset-password-dialog" :visible.sync="is_reset_password_show" title="重置密码" :before-close="handleClose">
        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item prop="password">
                <el-input type="password" v-model="form.password" placeholder="请输入新密码"></el-input>
            </el-form-item>
            <el-form-item prop="mobile">
                <el-input v-model="form.mobile" placeholder="请输入手机号码">
                    <span slot="suffix" class="get-code" @click="getCode('form')">{{get_code_text}}</span>
                </el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input v-model="form.code" placeholder="请输入短信验证码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="handleClose">取消</el-button>
                <el-button type="primary" @click="onSubmit('form')">确定</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import code from '@/api/code.js';
import register from '@/api/register.js';
import { validPhone } from '@/tool/validate.js';

export default {
    name: 'reset-password',
    props: ['is_reset_password_show'],
    data() {
        return {
            form: {
                password: undefined,
                mobile: undefined,
                code: undefined
            },
            rules: {
                password: [{
                    required: true, message: '请输入新密码', trigger: 'blur'
                }],
                mobile: [{
                    required: true, trigger: 'blur', validator: validPhone
                }],
                code: [{
                    required: true, message: '请输入短信验证码', trigger: 'blur'
                }]
            },
            count: 60,
            get_code_text: '获取短信验证码'
        }
    },
    methods: {
        getCode(formName) {
            this.$refs[formName].validateField('mobile', str => {
                if (str === '') {
                    code.getCode({mobile: this.form.mobile}).catch(err => {
                        this.$message.error(err);
                    });
                    this.setTime();
                }
            })
        },
        setTime() {
            if(this.count === 0) {
                this.get_code_text = '获取短信验证码';
                this.count = 60;
                return false;
            } else {
                this.get_code_text = this.count + 's';
                this.count --;
            }
            setTimeout(() => {
                this.setTime();
            }, 1000);
        },
        handleClose() {
            this.$refs['form'].resetFields();
            this.$emit('update:is_reset_password_show', false);
        },
        onSubmit(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    register.resetPassword(this.form).then(res => {
                        this.$message({
                            type: 'success',
                            message: '重置密码成功!'
                        });
                    }).catch(err => {
                        this.$message.error(err);
                    });
                    this.handleClose();
                } else {
                    return false;
                }
            });
        }
    }
}
</script>

<style>
.reset-password-dialog {
    width: 800px;
    margin: 15vh auto;
    text-align: center;
}
</style>
